<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
  <meta name="generator" content=
  "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">

  <title>I/O Constructors</title>
  <link href="../cppreference.css" rel="stylesheet" type="text/css">
</head>

<body>
<table>
  <tr>
  <td>
  <div class="body-content">

  <div class="header-box">
    <a href="../index.html">cppreference.com</a> &gt; <a href=
    "index.html">C++ I/O</a> &gt; <a href="constructors.html">I/O
    Constructors</a>
  </div>

  <div class="name-format">
    I/O Constructors
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;fstream&gt;
  fstream( const char *filename, openmode mode );
  ifstream( const char *filename, openmode mode );
  ofstream( const char *filename, openmode mode );
</pre>

  <p>The fstream, ifstream, and ofstream objects are used to do file
  I/O. The optional <em>mode</em> defines how the file is to be opened,
  according to the <a href="../io_flags.html#mode_flags">io stream mode
  flag</a>s. The optional <em>filename</em> specifies the file to be
  opened and associated with the stream.</p>

  <p>Input and output file streams can be used in a similar manner to
  C++ predefined I/O streams, cin and cout.</p>

  <div class="related-examples-format">
    Example code:
  </div>

  <div class="related-examples">
    <p>The following code reads input data and appends the result to an
    output file.</p>
    <pre class="example-code">
  ifstream fin( &quot;/tmp/data.txt&quot; );
  ofstream fout( &quot;/tmp/results.txt&quot;, ios::app );
  while( fin &gt;&gt; temp )
    fout &lt;&lt; temp + 2 &lt;&lt; endl;
  fin.close();
  fout.close();         
</pre>
  </div>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    <a href="close.html">close</a><br>
    <a href="open.html">open</a>
  </div>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
